import 'package:flutter/material.dart';
import 'package:gongge/utils/AudioUtils.dart';
import '../config/Config.dart';
import '../main.dart';
import '../pages/HomePage.dart';
import '../utils/ClickUtils.dart';

import '../utils/ PanPageRouteBuilder.dart';

//配置路由
final routes = {
  '/': (context) => HomePage(),
};

//固定写法
var onGenerateRoute = (RouteSettings settings) {
// 统一处理
  final String? name = settings.name;

  if (name != null) {
    //  final Function pageContentBuilder = routes[name]; 用下面的方法
    final Function pageContentBuilder = routes[name] as Function;
    if (settings.arguments != null) {
      final Route route = MaterialPageRoute(
          builder: (context) =>
              pageContentBuilder(context, arguments: settings.arguments));
      //等于
      /*     final Route route =
        MaterialPageRoute(
          builder: (context) {
            //将RouteSettings中的arguments参数取出来，通过构造函数传入
            return pageContentBuilder(context, arguments: settings.arguments);
          },
          settings: settings,
        );*/

      return route;
    } else {
      final Route route =
          MaterialPageRoute(builder: (context) => pageContentBuilder(context));
      return route;
    }
  }
  return null;
};

void startPageDown(BuildContext context, Widget widget) {
  if (ClickUtils.checkClick()) {
    Navigator.push(
        context,
        PanPageRouteBuilder(
            builder: (context) => widget, popDirection: AxisDirection.down));
  }
}

/**
 * 只能用于开启游戏
 */
void startPageDown2(BuildContext context, Widget widget,
    {CallBack? calllBack}) async {
  if (ClickUtils.checkClick()) {
    Config.isGame = true;
    await Navigator.push(
        context,
        PanPageRouteBuilder(
            builder: (context) => widget, popDirection: AxisDirection.down));
    Config.isGame = false;
    AudioUtils.getInstance()?.startOrStopBgm();
    if (calllBack != null) {
      calllBack(1);
    }
  }
}
